package com.ruoyi.system.service.impl.processServiceImpl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.process.*;
import com.ruoyi.system.mapper.process.*;
import com.ruoyi.system.service.processService.IProcessRibbonService;
import com.ruoyi.system.service.processService.IProcessShuntService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 分卷Service业务层处理
 * 
 * @author ljx
 * @date 2020-07-13
 */
@Service
public class ProcessShuntServiceImpl implements IProcessShuntService
{
    @Resource
    private ProcessShuntMapper processShuntMapper;
    @Resource
    private ProcessShuntRibbonMapper processShuntRibbonMapper;
    @Resource
    private ProcessShuntInventoryMapper processShuntInventoryMapper;
    @Resource
    private ProcessInventoryMapper processInventoryMapper;
    @Resource
    private ProcessRibbonInventoryMapper processRibbonInventoryMapper;
    @Resource
    private ProcessAluminizedInventoryMapper processAluminizedInventoryMapper;
    @Resource
    private ProcessRibbonMapper processRibbonMapper;
    @Resource
    private IProcessRibbonService iProcessRibbonService;


    /**
     * 查询分卷
     * 
     * @param id 分卷ID
     * @return 分卷
     */
    @Override
    public ProcessShunt selectProcessShuntById(Long id)
    {
        return processShuntMapper.selectProcessShuntById(id);
    }

    /**
     * 查询分卷列表
     * 
     * @param processShunt 分卷
     * @return 分卷
     */
    @Override
    public List<ProcessShunt> selectProcessShuntList(ProcessShunt processShunt)
    {
        return processShuntMapper.selectList(processShunt);
    }

    /**
     * 新增分卷
     * 
     * @param processShunt 分卷
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProcessShunt(ProcessShunt processShunt)
    {
        if(processShunt.getInventoryId() == null){
            return 0;
        }
        processShunt.setCreateTime(DateUtils.getNowDate());
        //添加分卷主表
        processShuntMapper.insertProcessShunt(processShunt);
        return this.add(processShunt);
    }

    public int add(ProcessShunt processShunt){
        //添加分卷和原膜关系表
        ProcessShuntInventory shuntInventory = new ProcessShuntInventory();
        shuntInventory.setShuntId(processShunt.getId());
        shuntInventory.setInventoryId(processShunt.getInventoryId());
        processShuntInventoryMapper.insertProcessShuntInventory(shuntInventory);
        //修改库存加工状态
        processInventoryMapper.updateStatus(4,Arrays.asList(processShunt.getInventoryId()));
        //添加分卷和并卷关系表
        ProcessShuntRibbon shuntRibbon = new ProcessShuntRibbon();
        shuntRibbon.setRibbonId(processShunt.getRibbonId());
        shuntRibbon.setShuntId(processShunt.getId());
        return processShuntRibbonMapper.insertProcessShuntRibbon(shuntRibbon);
    }

    /**
     * 修改分卷
     * 
     * @param processShunt 分卷
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProcessShunt(ProcessShunt processShunt)
    {
        processShunt.setUpdateTime(DateUtils.getNowDate());
        if(processShunt.getSign()==2){
            //查询历史绑定原膜,修改原膜编号状态
            List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByShuntId(processShunt.getId());
            List<Long> idLongs = inventories.stream().map(ProcessInventory::getId).collect(Collectors.toList());
            List<ProcessAluminizedInventory> list1 = processAluminizedInventoryMapper.selectAluminizedByInventoryId(idLongs);
            if(CollectionUtils.isNotEmpty(list1)){
                processInventoryMapper.updateStatus(3,idLongs);
            }else {
                processInventoryMapper.updateStatus(2,idLongs);
            }
            String[] de = new String[]{String.valueOf(processShunt.getId())};
            //删除分卷和原膜关联表
            processShuntInventoryMapper.deleteByShuntIds(de);
            //删除分卷和并卷关联表
            processShuntRibbonMapper.deleteByShuntIds(de);
            //添加分卷和并卷关联表
            //添加分卷和原膜关联表
            //修改原膜编号
            this.add(processShunt);
        }
        return processShuntMapper.updateProcessShunt(processShunt);
    }

    /**
     * 删除分卷对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResult deleteProcessShuntByIds(String ids)
    {
        String[] strings = Convert.toStrArray(ids);
        Boolean flag = false;
        List<String> delete = new ArrayList<>();
        //查询并卷和镀铝和加工库存是否有数据，都没有则是未加工
        for (String id : strings) {
            Long aLong = Long.valueOf(id);
            List<ProcessInventory> inventories = processInventoryMapper.selectInventoryByShuntId(aLong);
            if(CollectionUtils.isNotEmpty(inventories)){
                //分卷原膜一对一,分卷对应的原膜不是分卷加工状态，不允许删除
                if(inventories.get(0).getStatus()!=4){
                    flag = true;
                    continue;
                }
                delete.add(id);
                //修改原膜加工状态：
                // 1查询原膜和镀铝是否有关联关系，有则原膜加工状态改为镀铝。
                // 2和镀铝无关联关系，将原膜加工状态改为并卷
                List<Long> idLongs = inventories.stream().map(ProcessInventory::getId).collect(Collectors.toList());
                List<ProcessAluminizedInventory> list1 = processAluminizedInventoryMapper.selectAluminizedByInventoryId(idLongs);
                if(CollectionUtils.isNotEmpty(list1)){
                    processInventoryMapper.updateStatus(3,idLongs);
                }else {
                    processInventoryMapper.updateStatus(2,idLongs);
                }
            }else {
                //inventories为0表示没有绑定的原膜
                delete.add(id);
            }
        }
        if(CollectionUtils.isNotEmpty(delete)){
            String[] de = new String[delete.size()];
            de = delete.toArray(de);
            //删除分卷和原膜关联表
            processShuntInventoryMapper.deleteByShuntIds(de);
            //删除分卷和并卷关联表
            processShuntRibbonMapper.deleteByShuntIds(de);
            //删除分卷信息
            processShuntMapper.deleteProcessShuntByIds(de);
        }
        if(flag){
            return AjaxResult.success("原膜已加工无法删除");
        }
        return AjaxResult.success();
    }

    /**
     * 删除分卷信息
     * 
     * @param id 分卷ID
     * @return 结果
     */
    @Override
    public int deleteProcessShuntById(Long id)
    {
        return processShuntMapper.deleteProcessShuntById(id);
    }

    @Override
    public List<ProcessRibbon> getRibbonForAdd() {
        //获得到分卷已绑定的原膜id
        List<Long> shuntInventoryIds = processShuntInventoryMapper.getAllShuntInventoryIds()
                .stream().map(ProcessShuntInventory::getInventoryId).collect(Collectors.toList());
        return this.getRibbonForWhere(shuntInventoryIds);
    }

    @Override
    public List<ProcessRibbon> getRibbonForWhere(List<Long> ids) {
        ProcessRibbon processRibbon = new ProcessRibbon();
        //获取并卷对应的原膜属于非未加工，且非出库状态
        processRibbon.setType(1);
        processRibbon.setIdList(ids);
        List<ProcessRibbon> ribbons = processRibbonMapper.getRibbonWithInventory(processRibbon);
        if(CollectionUtils.isNotEmpty(ribbons)){
            ribbons.stream().forEach(r -> {
                String[] split = r.getInventoryIds().split(",");
                if(split.length>0){
                    List<Long> objects = new ArrayList<>();
                    for (int i = 0; i < split.length; i++) {
                        objects.add(Long.valueOf(split[i]));
                    }
                    r.setInventories(processInventoryMapper.selectByIds(objects));
                }
            });
        }
        return ribbons;
    }
}
